//
//  HMNewFeatrueCollectionViewController.swift
//  HMWeibo
//
//  Created by wulei on 15/12/10.
//  Copyright © 2015年 wulei. All rights reserved.
//

import UIKit
import SnapKit
// 可重用ID
private let HMNewFeatrueCellID = "NewFeatrueCell"

// 新特性图片的数量
private let HMNewFeatrueImageCount = 4

class HMNewFeatrueCollectionViewController: UICollectionViewController {
    
    // 懒加载 Layout
    // 这里不能用懒加载 ,懒加载属性必须在控制器实例化之后才会被创建
   private lazy var layout = UICollectionViewFlowLayout()
    
    
    // MARK: - 构造函数
    init() {
        let layout = UICollectionViewFlowLayout()
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = .Horizontal
        
        
        // super.指定的构造函数
        // 构造函数完成之后,内部属性才会被创建
        super.init(collectionViewLayout: layout)
        
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prefersStatusBarHidden() ->Bool {
        return true
    }

    

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(HMNewFeatrueCell.self, forCellWithReuseIdentifier: HMNewFeatrueCellID)


    }




    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return HMNewFeatrueImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(HMNewFeatrueCellID, forIndexPath: indexPath) as! HMNewFeatrueCell
    
        cell.backgroundColor = indexPath.item % 2 == 0 ? UIColor.redColor() : UIColor.greenColor()
        
        cell.imageIndex = indexPath.item
    
        return cell
    }

// scrollView 停止滚动方法
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
     
        // 计算页数
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        
        // 判断是不是最后一页
        if page != HMNewFeatrueImageCount - 1 {
            return
        }
        // 播放动画
        let indexPath: NSIndexPath = NSIndexPath(forItem: page, inSection: 0)
        let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! HMNewFeatrueCell
        
        cell.showButtonAnim()
        
        
        
        
        
    }
    
}

// MARK: - 自定义新特性Cell
private class HMNewFeatrueCell: UICollectionViewCell {
    
    private var imageIndex: Int = 0 {
        didSet{
            iconImage.image = UIImage(named: "new_feature_\(imageIndex+1)")
            startButton.hidden = true
        }
    }
    
    // 按钮的动画效果
    private func showButtonAnim() {
        
        startButton.hidden = false
        
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
    UIView.animateWithDuration(1.6, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 10, options: [], animations: { () -> Void in
        
        self.startButton.transform = CGAffineTransformIdentity
        }) { (_) -> Void in
            print("OK")
        }
        
        
        startButton .addTarget(self, action: "clickStartButton", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // 用户点击事件
    @objc private func clickStartButton() {
        NSNotificationCenter.defaultCenter().postNotificationName(WBSwitchRootViewControllerNotification, object: nil)
    }
    
    
    
    
    func setupUI() {
        addSubview(iconImage)
        addSubview(startButton)
        iconImage.frame = bounds
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.bottom.equalTo(self.snp_bottom).multipliedBy(0.7)
        }
    }
    
    
    //MARK: - 懒加载控件 
    // 图像
    private var iconImage: UIImageView = UIImageView()
    
    // 按钮
    private var startButton: UIButton = UIButton(title: "开始体验", color: UIColor.whiteColor(), backimageName: "new_feature_finish_button")
    
    
    
    
    
}